﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DotNet.WebUI.BasicControl
{
    public partial class Login_Cookie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次加载页面是执行
            if (!IsPostBack)
            {
                //检查Cookie是否存在
                if (Request.Cookies["Soft2014_Login"] != null)
                {
                    //获取Cookie对象
                    HttpCookie objCookie = Request.Cookies["DingY.Cookie"];
                    //使用Cookie的具体值
                    if (objCookie["LoginID"] == "admin" && objCookie["Pwd"] == "123456")
                    {
                        Response.Redirect("Home.aspx");
                    }
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtLoginID.Text == "admin" && txtPwd.Text == "123456")
            {
                //记录登录信息
                if (ChkJiL.Checked == true)
                {
                    //定义Cookie对象
                    HttpCookie objCookie = new HttpCookie("DingY.Cookie");
                    //设置Cookie的具体值
                    objCookie["LoginID"] = txtLoginID.Text;
                    objCookie["Pwd"] = txtPwd.Text;
                    //设置有效期
                    objCookie.Expires = DateTime.Now.AddDays(10);
                    //存储在本地
                    Response.Cookies.Add(objCookie);
                }

                Response.Redirect("Home.aspx");
            }
        }
    }
}